﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects.DataClasses;
using System.Threading.Tasks;
using System.Linq.Expressions;

namespace JeanFish.Data
{
    /// <summary>
    /// 表示公用数据CUID接口。
    /// </summary>
    /// <typeparam name="TEntity">实体类型</typeparam>
    public interface ICommonDataAccess<TEntity>
        where TEntity : EntityObject, new()
    {
        /// <summary>
        /// 获取指定查询条件的第一条数据，或者返回默认值。
        /// </summary>
        /// <param name="whereClause">查询条件</param>
        /// <param name="detached">读取的实体是否与实体框架分离</param>
        /// <param name="defaultEntity">默认值</param>
        /// <returns>符合查询条件的第一条数据或默认值</returns>
        TEntity Read(Expression<Func<TEntity, bool>> whereClause,bool detached=false, TEntity defaultEntity = null);
        
        /// <summary>
        /// 在数据库中插入新实体。
        /// </summary>
        /// <param name="entity">新实体</param>
        /// <returns>操作结果</returns>
        int Create(TEntity entity);
        /// <summary>
        /// 在数据库中更新实体。
        /// </summary>
        /// <param name="entity">要更新的实体</param>
        /// <returns>操作结果</returns>
        int Update(TEntity entity);
        /// <summary>
        /// 在数据库中删除实体。
        /// </summary>
        /// <param name="entity">要删除的实体</param>
        /// <returns>操作结果</returns>
        int Delete(TEntity entity);
        /// <summary>
        /// 根据查询条件列举实体集合。
        /// </summary>
        /// <param name="whereClause">查询条件，null则不筛选</param>
        /// <param name="orderbyClause">排序条件，null则不排序</param>
        /// <param name="Ascend">升序或降序，排序条件可用时生效</param>
        /// <param name="pageSize">分页大小，0表示不分页</param>
        /// <param name="pageIndex">分页开始索引，分页大小可用时生效</param>
        /// <param name="total">所有实体总数</param>
        /// <returns>实体集合</returns>
        IQueryable<TEntity> List(Expression<Func<TEntity, bool>> whereClause, Expression<Func<TEntity, object>> orderbyClause, bool Ascend, int pageSize, int pageIndex, out int total);
    }
}